package com.bbs.toutiao.exception;


import com.bbs.toutiao.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletResponse;

/**
 * 描述: 统一异常拦截
 * 作者： zhangBin
 * 时间： 2023/9/17 10:42
 * 版本： 1.0
 */

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BaseException.class)
    public Result exceptionHandler(BaseException e) {
        log.error("报错了=======》"+String.valueOf(e));
        return Result.error(e.getCode(), e.getMessage());
    }
    @ExceptionHandler(NoHandlerFoundException.class)
    public ResponseEntity<String> handleNoHandlerFoundException(NoHandlerFoundException ex) {
        // 在这里处理异常，例如返回自定义的错误信息
        return new ResponseEntity<>("请求的接口不存在", HttpStatus.NOT_FOUND);
    }

}
